<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <script src="jquery-1.9.1.js"></script>
    <title>ajax测试</title>
    <style></style>
</head>
<body>
<div class="load-div"><h2>ajax</h2></div>
<br/>

<a id="a_upload">上传文件</a>
<input id="input_upload_file" type="file" name="hruser_files" style="display:none" onchange="uploadFileS();" />


    　
<script>
    //将a标签绑定input标签
    $('#a_upload').click(function(){
        $('#input_upload_file').click()
    });
    function uploadFileS(){
        var files = $('#input_upload_file').prop('files');
        var data = new FormData();
        data.append('file', files[0]);
        data.append('id', 111);
        data.append('name', "AAA");
        var queryParamsJsonObject={//封装暂时不支持中文
            queryParamsA:222,
            queryParamsB:"BBB"
        };
        ajaxPostMultipartFormDataRequest("./hello/ajaxPostMultipartFormDataRequest",data,queryParamsJsonObject);
    }
</script>

</body>
</html>
<script>

setTimeout('test()', 400);

function test() {
    var jsonObject={
      id:111,
      name:"AAA"
    };
    var queryParamsJsonObject={//封装暂时不支持中文
        queryParamsA:222,
        queryParamsB:"BBB"
    };
    ajaxGetRequest("./hello/ajaxGetRequest",jsonObject);

    ajaxPostJsonRequest("./hello/ajaxPostJsonRequest",jsonObject,queryParamsJsonObject);

    ajaxPostFormUrlEncodedRequest("./hello/ajaxPostFormUrlEncodedRequest",jsonObject,queryParamsJsonObject);

}





//应用后台通用结果处理
function commomCheckLogin(responseJsonStr) {
    if (responseJsonStr.status != 1200) {
        alert("error message:"+responseJsonStr.message);
        return false;
    } else {
        return true;
    }
}
function errorDeal(methd,jqXHR, textStatus,errorThrown ){
    alert("error:"+methd+" Response jqXHR:"+JSON.stringify(jqXHR));
    alert("error:"+methd+" Response textStatus:"+JSON.stringify(textStatus));
    alert("error:"+methd+" Response errorThrown:"+JSON.stringify(errorThrown));
}


function ajaxGetRequest(reqUrl,jsonObject){
    $.ajax({
               type: "GET",
               url: reqUrl,
               data: jsonObject,
               async: true,
               dataType: "json",
               success:function(data, textStatus,jqXHR){
                   if(!commomCheckLogin(data)){
                       return false;
                   }
                   console.info("ajaxGet Response data:"+JSON.stringify(data));
               },
               complete:function(jqXHR, textStatus ){
               },
               error: function(jqXHR, textStatus,errorThrown ){
                   errorDeal("ajaxGetRequest",jqXHR, textStatus,errorThrown );
               }
           });
}
//Content-Type: application/json
function ajaxPostJsonRequest(reqUrl,jsonObject,queryParamsJsonObject){
    //问号拼接参数
    var queryParamsArray=new Array();
    for (var attribute in queryParamsJsonObject) {
        queryParamsArray.push(attribute+"="+queryParamsJsonObject[attribute]);
    }
    var queryParamsString="";
    if(reqUrl.indexOf("?")!=-1){
        queryParamsString="&"+queryParamsArray.join("&");
    }else{
        queryParamsString="?"+queryParamsArray.join("&");
    }
    $.ajax({
               type: "POST",
               url: reqUrl+queryParamsString,
               data: JSON.stringify(jsonObject),
               contentType:"application/json",
               async: true,
               dataType: "json",
               success:function(data, textStatus,jqXHR){
                   if(!commomCheckLogin(data)){
                       return false;
                   }
                   console.info("ajaxPostJson Response data:"+JSON.stringify(data));
               },
               complete:function(jqXHR, textStatus ){
               },
               error: function(jqXHR, textStatus,errorThrown ){
                   errorDeal("ajaxPostJson",jqXHR, textStatus,errorThrown );
               }
           });
}
//Content-Type: application/x-www-form-urlencoded
function ajaxPostFormUrlEncodedRequest(reqUrl,jsonObject,queryParamsJsonObject){
    //问号拼接参数
    var queryParamsArray=new Array();
    for (var attribute in queryParamsJsonObject) {
        queryParamsArray.push(attribute+"="+queryParamsJsonObject[attribute]);
    }
    var queryParamsString="";
    if(reqUrl.indexOf("?")!=-1){
        queryParamsString="&"+queryParamsArray.join("&");
    }else{
        queryParamsString="?"+queryParamsArray.join("&");
    }
    $.ajax({
               type: "POST",
               url: reqUrl+queryParamsString,
               data: jsonObject,
               contentType:"application/x-www-form-urlencoded",
               async: true,
               dataType: "json",
               success:function(data, textStatus,jqXHR){
                   if(!commomCheckLogin(data)){
                       return false;
                   }
                   console.info("ajaxFormUrlEncoded Response data:"+JSON.stringify(data));
               },
               complete:function(jqXHR, textStatus ){
               },
               error: function(jqXHR, textStatus,errorThrown ){
                   errorDeal("ajaxFormUrlEncoded",jqXHR, textStatus,errorThrown );
               }
           });
}
// //Content-Type: multipart/form-data;
function ajaxPostMultipartFormDataRequest(reqUrl,jsonObject,queryParamsJsonObject){
    //问号拼接参数
    var queryParamsArray=new Array();
    for (var attribute in queryParamsJsonObject) {
        queryParamsArray.push(attribute+"="+queryParamsJsonObject[attribute]);
    }
    var queryParamsString="";
    if(reqUrl.indexOf("?")!=-1){
        queryParamsString="&"+queryParamsArray.join("&");
    }else{
        queryParamsString="?"+queryParamsArray.join("&");
    }

    $.ajax({
               type: "POST",
               url: reqUrl+queryParamsString,
               data: jsonObject,
               processData: false,
               contentType: false,
               async: true,
               dataType: "json",
               success:function(data, textStatus,jqXHR){
                   if(!commomCheckLogin(data)){
                       return false;
                   }
                   console.info("ajaxFormUrlEncoded Response data:"+JSON.stringify(data));
               },
               complete:function(jqXHR, textStatus ){
               },
               error: function(jqXHR, textStatus,errorThrown ){
                   errorDeal("ajaxFormUrlEncoded",jqXHR, textStatus,errorThrown );
               }
           });
}
</script>